package com.seocoo.worker.order.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.seocoo.worker.R;
import com.seocoo.worker.order.entity.RspGoodsInfo;
import com.seocoo.worker.order.widget.adapter.OrderDetailCommodityAdapter;

import java.util.List;

/***
 *订单详情商品信息布局
 */
public class OrderDetailCommodityView extends ConstraintLayout implements View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private RecyclerView recycleView;
    private TextView tv_count;
    private ImageView iv_expand;
    private TextView tv_expand;
    private TextView tv_score_name;
    private LinearLayout ll_expand;
    private OrderDetailCommodityAdapter mAdapter;
    //当前是否折叠 默认不折叠
    private boolean expand = true;

    public OrderDetailCommodityView(Context context) {
        super(context);
        initView(context);
    }

    public OrderDetailCommodityView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public OrderDetailCommodityView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = inflate(context, R.layout.order_detail_commodity_layout, this);
        recycleView = view.findViewById(R.id.recycler_view);
        tv_count = view.findViewById(R.id.tv_count);
        iv_expand = view.findViewById(R.id.iv_expand);
        tv_expand = view.findViewById(R.id.tv_expand);
        ll_expand = view.findViewById(R.id.ll_expand);
        tv_score_name = view.findViewById(R.id.tv_score_name);
        mAdapter = new OrderDetailCommodityAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        mAdapter.bindToRecyclerView(recycleView);
        mAdapter.setOnItemChildClickListener(this);
        recycleView.setAdapter(mAdapter);
        ll_expand.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    public void initData(List<RspGoodsInfo> data, String status) {
        if (mAdapter == null) {
            return;
        }
        tv_count.setText("共" + data.size() + "件商品");
        mAdapter.setStatus(status);
        mAdapter.setNewData(data);
    }

    /**
     * 商店名称
     *
     * @param scoreName
     */
    public void setScoreName(String scoreName) {
        tv_score_name.setText(scoreName);
    }

    public BaseQuickAdapter getAdapter() {
        return this.mAdapter;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_expand) {
            if (expand) {
                expand = false;
                tv_expand.setText("展开");
                recycleView.setVisibility(GONE);
                iv_expand.setBackgroundResource(R.drawable.base_arrow_gray_down);
            } else {
                expand = true;
                tv_expand.setText("收起");
                recycleView.setVisibility(VISIBLE);
                iv_expand.setBackgroundResource(R.drawable.base_arrow_gray_up);
            }
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (view.getId() == R.id.iv_expand_goods) {
            List data = adapter.getData();
            if (data.isEmpty()) {
                return;
            }
            RspGoodsInfo item = (RspGoodsInfo) data.get(position);
            if (mAdapter != null && item != null) {
                mAdapter.setGoodsExpand(position, item);
            }
        }
    }
}
